let BaseController = require('./BaseController')
let fs = require('fs')
let path = require('path')
class Controller extends BaseController {
    constructor() {
        super();
        this.baseSrc = `d:/数据指标分类/${new Date().format('yyyyMMdd')}/待入库/10公告/分红/`;
        this.encode = 'GBK'
        this.params = {
            pageIndex: '',
            pageTotal: ''
        }
        this.data = []

        this.dateList = [
            '2018-06-30',
            '2018-03-31',
            '2017-12-31',
            '2017-09-30',
            '2017-06-30',
            '2017-03-31',
            '2016-12-31',
            '2016-09-30',
            '2016-06-30',
            '2016-03-31',
            '2015-12-31',
            '2015-09-30',
            '2015-06-30',
            '2015-03-31',
            '2014-12-31'
        ]
        this.dateIndex = 0;
    }
    /**
     * 获取url
     */
    getUrl(date) {
        //http://data.eastmoney.com/yjfp/
        return `http://data.eastmoney.com/DataCenter_V3/yjfp/getlist.ashx?js=var%20wnPdAQne&pagesize=50&page=${this.params.pageIndex}&sr=-1&sortType=YAGGR&mtk=%C8%AB%B2%BF%B9%C9%C6%B1&filter=(ReportingPeriod=^${this.params.date}^)&rt=51118159`
    }

    /**
     * 获取历史数据
     */
    async getHistory() {
        console.log('37公告数据_分红Controller')
        if (this.dateIndex < this.dateList.length) {
            this.params.date = this.dateList[this.dateIndex]
            if (this.params.pageTotal === '' || this.params.pageIndex <= this.params.pageTotal) {
                let result = await this.getData('', this.isCache)
                result = this.axiosDataFormat(result.toString());
                let pageDate = result.data;
                this.params.pageTotal = result.pages || 0
                console.log(JSON.stringify(this.params))
                this.data = [...this.data, ...pageDate]
                this.params.pageIndex++;
            } else {
                this.writeCsv(this.data)
                this.data = [];
                this.params.pageIndex = ''
                this.dateIndex++;
            }
            await this.getHistory()
        }

    }
    /**
     * 数据格式化
     */
    axiosDataFormat(data) {
        let result = null
        try {
            result = JSON.parse(data.replace(/^var\s\w+?\s*\=/, '').trim())
        } catch (err) {
            try {
                result = eval('(' + data.replace(/^var\s\w+?\s*\=/, '').trim() + ')')
            } catch (err) {
                if (data.indexOf('502 Bad Gateway') > -1) {
                    return []
                } else {
                    console.log(err)
                }

            }
        }
        return result
    }

    /**
     * 写csv
     */
    writeCsv(arr) {
        if (!fs.existsSync(this.baseSrc)) {
            this.mkdirs(this.baseSrc)
        }
        if (arr && arr.length) {
            fs.writeFileSync(path.join(this.baseSrc, `分红配股_${this.params.date.replace(/-/g, '')}.csv`), this.convertToCSV(arr, 'title'), 'UTF-8')
        }
    }

    async main(startDate = '2015-01-01', isCache = true) {
        this.isCache = isCache;
        console.log('--->37公告数据_分红Controller')
        await this.getHistory()
        this.writeCsv(this.data);
    }
}

let controller = new Controller()
module.exports = controller
